import Vue from "vue";
import Router from "vue-router";
import { routes } from "./routes";

Vue.use(Router);

const router = new Router({
  // mode: "history",
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition && to.meta.keepAlive) {
      // return new Promise((resolve) => {
      //   setTimeout(() => {
      //     resolve(savedPosition);
      //   }, 200);
      // });
      return savedPosition;
    } else {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ x: 0, y: 0 });
        }, 5);
      });
    }
  },
});
// router.beforeEach((from, to, next) => {
//   if (to.meta.requireAuth) { // 判断跳转的路由是否需要登录
//     if (localStorage.getItem("token")) { // vuex.state判断token是否存在
//       next() // 已登录
//     } else {
//       next({
//         path: '/login',
//         query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
//       })
//     }
//   } else {
//     next();
//   }
// })

export default router;
